package com.pansoft.openplanet.activity;

import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.efounder.frame.baseui.BaseFragment;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.fragment.MyResourceFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 保险柜
 *
 * @author YQS
 */
public class SafeBoxActivity extends TCBaseActivity implements View.OnClickListener {

    private static final String TAG = "SafeBoxActivity";
    private ImageView ivBack;
    private TabLayout tablayout;
    private ViewPager viewpager;
    private String[] titles = new String[]{ResStringUtil.getString(R.string.open_planet_asset_safe_box_my_res), ResStringUtil.getString(R.string.open_planet_asset_safe_box_purchased_res)};
    private List<BaseFragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_chain_activity_safe_box);
        initView();
        initData();

    }

    private void initData() {
        fragments = new ArrayList<>();
        BaseFragment baseFragment1 = new MyResourceFragment("my");
        fragments.add(baseFragment1);
        BaseFragment baseFragment2 = new MyResourceFragment("buyed");
        fragments.add(baseFragment2);
        initViewPager();
        initTabLayout();
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        }
    }

    private void initViewPager() {
        viewpager.setAdapter(new FixedPagerAdapter(getSupportFragmentManager()));
    }

    private void initTabLayout() {
//        for (int i = 0; i < titles.length; i++) {
//            tablayout.addTab(tablayout.newTab().setText(titles[i]));
//        }
        tablayout.setupWithViewPager(viewpager);


        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getPosition();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    public class FixedPagerAdapter extends FragmentStatePagerAdapter {
        private FragmentManager fm;

        public FixedPagerAdapter(FragmentManager fm) {
            super(fm);
            this.fm = fm;
        }


        @Override
        public BaseFragment getItem(int position) {
            return fragments.get(position);
        }


        @Override
        public int getCount() {
            return fragments.size();
        }

//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            KlineBaseFragment fragment = null;
//            try {
//                removeFragment(container, position);
//                fragment = (KlineBaseFragment) super.instantiateItem(container, position);
//            } catch (Exception e) {
//
//            }
//            return fragment;
//        }
//
//        private void removeFragment(ViewGroup container, int index) {
//            String tag = getFragmentTag(container.getId(), index);
//            Fragment fragment = fm.findFragmentByTag(tag);
//            if (fragment == null)
//                return;
//            FragmentTransaction ft = fm.beginTransaction();
//            ft.remove(fragment);
//            ft.commit();
//            ft = null;
//            fm.executePendingTransactions();
//        }
//
//        private String getFragmentTag(int viewId, int index) {
//            try {
//                Class<FragmentPagerAdapter> cls = FragmentPagerAdapter.class;
//                Class<?>[] parameterTypes = {int.class, long.class};
//                Method method = cls.getDeclaredMethod("makeFragmentName",
//                        parameterTypes);
//                method.setAccessible(true);
//                String tag = (String) method.invoke(this, viewId, index);
//                return tag;
//            } catch (Exception e) {
//                e.printStackTrace();
//                return "";
//            }
//        }
//
//        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
//
//        }

        @Override
        public CharSequence getPageTitle(int position) {
            String tname = titles[position];
            return tname;
        }
    }
}
